package com.web.controller;

import com.web.entity.PageResult;
import com.web.entity.Result;
import com.web.entity.User;
import com.web.service.UserService;
import com.web.utils.Code;
import com.web.utils.RedisUtil;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Slf4j
@RequestMapping("/user")
@RestController
@CrossOrigin
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private Code code;

    @Autowired
    private RedisUtil redisUtil;

    //http://localhost:8080/user/checkCode2
    @GetMapping("/checkCode2")
    public void checkcode2(HttpServletRequest request, HttpServletResponse response){
        response.setContentType("image/jpeg");
        response.setDateHeader("Expires",0);
        response.setHeader("Pragma","no-cache");
        response.setHeader("Cache-Control","no-cache");
        //引用验证码工具类，生成验证码
        System.out.println("验证码："+code);
        code.getValidateCode(request,response);
    }

    //http://localhost:8080/user/getToken
    @GetMapping("/getToken")
    public Result getToken(){
        log.info("获取验证码");
        System.out.println("redisz验证码："+redisUtil.get("RANDOMCODEKEY"));

        if (redisUtil.get("RANDOMCODEKEY")== null){
            return Result.success("当前验证码已过期，请重新加载");
        }
        return Result.success(redisUtil.get("RANDOMCODEKEY"));
    }



    //http://localhost:8080/user/updateRealNameAndIntroduce
    @PutMapping("/updateRealNameAndIntroduce")
    public Result updateRealName(@RequestBody User user){
        log.info("修改用户名:{}",user);
        userService.updateRealNameAndIntroduce(user);
        return Result.success("修改成功");
    }

    //http://localhost:8080/user/updatePhone
    @PostMapping("/updatePhone")
    public Result updatePhone(@RequestBody User user){
        log.info("修改手机号:{}",user);
        userService.updatePhone(user);
        return Result.success("修改成功");
    }

    //http://localhost:8080/user/updateEmail
    @PostMapping("/updateEmail")
    public Result updateEmail(@RequestBody User user){
        log.info("修改邮箱:{}",user);
        userService.updateEmail(user);
        return Result.success("修改成功");
    }

    //http://localhost:8080/user/updatePassword
    @GetMapping("/updatePassword")
    public Result updatePassword( User user , String oldPassword){

        log.info("修改密码:{}",user);
        if(!userService.updatePassword(user,oldPassword)){
            return Result.error("旧密码错误");
        }
        return Result.success("修改成功");
    }

    @GetMapping("/page")
    public Result page(Integer page,
                       Integer pageSize,
                       User user){
        log.info("分页查询, {}", user);
        PageResult<User> pageResult = userService.page(page,pageSize,user);
        return Result.success(pageResult);
    }

    //http://localhost:8080/user/1
    @GetMapping("/{id}")
    public Result findById(@PathVariable Integer id){
        log.info("根据id查询到用户：{}",id);
        User user = userService.findById(id);
        return Result.success(user);
    }


    @PutMapping("/update")
    public Result update(@RequestBody User user){
        log.info("根据id修改用户{}",user);
        userService.update(user);
        return Result.success();
    }
//http://localhost:8080/user/save
    @PostMapping("/save")
    public Result insert(@RequestBody User user){
        log.info("新增用户:username={},password={}",user.getUsername(),user.getPassword());
        userService.insert(user);
        return Result.success("新增成功");
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id){
        log.info("删除用户:{}",id);
        if (id==null){
            return Result.error("用户ID不能为空");
        }
        userService.delete(id);
        return Result.success();
    }
}
